class ChipController {
    constructor($rootScope, $scope, $cookieStore, $timeout, $state, ngDialog, dialogService, ChipService, iRunner){

    	var vm = this;

    	vm.$rootScope    = $rootScope;
    	vm.$cookieStore  = $cookieStore;
    	vm.$timeout 	 = $timeout;
    	vm.$state   	 = $state;
    	vm.ngDialog 	 = ngDialog;
    	vm.dialogService = dialogService;
    	vm.ChipService   = ChipService;
    	vm.iRunner 	     = iRunner;

        // 芯片厂商列表
        vm.chip_maker_list = vm.iRunner.chipMakerConfig;

    	// 表单数据初始化
        vm.chip = {
            bib: '',
            chip_no: '',
            chip_maker: '',
            chip_mac: ''
        }

        // 表单提交
        vm.submitForm = function(isValid) {

            // 验证失败, 提示用户
            if (!isValid) {
                
                if (!vm.chip.chip_no) {
                    $.blink(angular.element(document.getElementsByName('chipNo')), 5, 100)
                } else if (!vm.chip.chip_maker) {
                    $.blink(angular.element(document.getElementsByName('chipMaker')), 5, 100)
                } else if (!vm.chip.chip_mac) {
                    $.blink(angular.element(document.getElementsByName('chipMac')), 5, 100)
                }

            }

            // 验证通过
            if (isValid) {

                // 拼接POST数据格式
                var params = {
                    bib: vm.chip.bib,
                    chip_no: vm.chip.chip_no,
                    chip_maker: vm.chip.chip_maker.name,
                    chip_mac: vm.chip.chip_mac
                }

                console.log(params)

                // 创建芯片
                vm.ChipService.postChipCreate(params, vm.$cookieStore.get('access_token'), vm.$state.params.race_id)
                .then(function(data) {

                    // 成功调用
                    data.code==0&&vm.dialogService.success('创建成功', 'dashboard.race.chiplist');

                    // 调用失败, 提示用户
                    (data.code!=0&&data.code!=-1001)&&vm.dialogService.error(data.message);

                    // 登录信息已过期, 请重新登录
                    data.code==-1001&&vm.dialogService.error(data.message, true);

                    console.log(data)
                }, function(data) {
                    console.log('error')
                });
            }

            return false;
        }

    }
}

ChipController.$inject = ['$rootScope', '$scope', '$cookieStore', '$timeout', '$state', 'ngDialog', 'dialogService', 'ChipService', 'iRunner']; 

export default ChipController;